/**
 *  Copyright 2006-2007 EmbedChat
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.javabb.bbcode;

/**
 * author: <a href="mailto:erka.mercy@gmail.com">erka</a>
 */
class MutableCharSequence implements CharSequence {
    /** */
    public CharSequence base;

    /** */
    public int start;

    /** */
    public int length;

    /** */
    public String param = null;

    /**
     */
    public MutableCharSequence() {
        //
    }

    /**
     * @param base
     * @param start
     * @param length
     */
    public MutableCharSequence(CharSequence base, int start, int length) {
        reset(base, start, length);
    }

    /**
     * @see java.lang.CharSequence#length()
     */
    public int length() {
        return this.length;
    }

    /**
     * @see java.lang.CharSequence#charAt(int)
     */
    public char charAt(int index) {
        return this.base.charAt(this.start + index);
    }

    /**
     * @see java.lang.CharSequence#subSequence(int,int)
     */
    public CharSequence subSequence(int pStart, int end) {
        return new MutableCharSequence(this.base,
                this.start + pStart,
                this.start + (end - pStart));
    }

    /**
     * @param pBase
     * @param pStart
     * @param pLength
     * @return -
     */
    public CharSequence reset(CharSequence pBase, int pStart, int pLength) {
        this.base = pBase;
        this.start = pStart;
        this.length = pLength;

        return this;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();

        for (int i = this.start; i < (this.start + this.length); i++) {
            sb.append(this.base.charAt(i));
        }

        return sb.toString();
    }

}

